/* Copyright 2009
 *
 * This program and the accompanying materials
 * are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies
 * this distribution, and is available at
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * Contributors:
 * 	   IBM Corporation - initial API and implementation for JDT/DLTK
 *     Sean W. Quinn - initial adoption for use with PHP from various sources.
 */
package org.eclipse.php.internal.ui.preferences.formatter;

import java.util.List;
import java.util.Observable;
import java.util.Observer;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.dltk.ui.formatter.FormatterModifyDialog;
import org.eclipse.dltk.ui.formatter.IProfile;
import org.eclipse.dltk.ui.formatter.IProfileManager;
import org.eclipse.dltk.ui.formatter.IProfileStore;
import org.eclipse.php.internal.ui.preferences.PreferencesAccess;
import org.eclipse.php.internal.ui.preferences.formatter.profile.PHPFormatterProfileManager;
import org.eclipse.php.internal.ui.preferences.formatter.profile.PHPFormatterProfileStore;
import org.eclipse.php.internal.ui.preferences.formatter.profile.PHPProfile;
import org.eclipse.php.internal.ui.preferences.formatter.profile.PHPProfileManager;
import org.eclipse.php.internal.ui.preferences.formatter.profile.PHPProfileStore;
import org.eclipse.php.internal.ui.preferences.formatter.profile.PHPProfileVersioner;
import org.eclipse.php.internal.ui.util.ControlUtils;
import org.eclipse.php.ui.PHPUiHelper;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;

/**
 * @author Sean W. Quinn (swquinn@gmail.com)
 *
 */
public class PHPCodeFormatterConfigurationBlock extends ProfileConfigurationBlock {

  /**
   *
   * @author Sean W. Quinn (swquinn@gmail.com)
   */
  private class PreviewController implements Observer {

    public PreviewController(PHPProfileManager profileManager) {
      profileManager.addObserver(this);
      //phpPreview.setWorkingValues(profileManager.getSelected().getSettings());
      //phpPreview.update();
    }

    public void update(Observable o, Object arg) {
      final int value = ((Integer) arg).intValue();
      switch (value) {
        case PHPProfileManager.PROFILE_CREATED_EVENT:
        case PHPProfileManager.PROFILE_DELETED_EVENT:
        case PHPProfileManager.SELECTION_CHANGED_EVENT:
        case PHPProfileManager.SETTINGS_CHANGED_EVENT:
          //phpPreview.setWorkingValues(((ProfileManager) o).getSelected().getSettings());
          //phpPreview.update();
      }
    }

  }

  /** */
  private static final String DIALOGSTORE_LASTSAVELOADPATH = PHPUiHelper.UI_PLUGIN_ID + ".codeformatter"; //$NON-NLS-1$

  private static final String FORMATTER_DIALOG_PREFERENCE_KEY = "formatter_page"; //$NON-NLS-1$
  /**
   * Some PHP source code used for preview.
   */
  protected final String PREVIEW = "/**\n* " + //$NON-NLS-1$
      PHPFormatterMessages.CodingStyleConfigurationBlock_preview_title + "\n*/\n\n" + //$NON-NLS-1$
      "package mypackage; import java.util.LinkedList; public class MyIntStack {" + //$NON-NLS-1$
      "private final LinkedList fStack;" + //$NON-NLS-1$
      "public MyIntStack(){fStack= new LinkedList();}" + //$NON-NLS-1$
      "public int pop(){return ((Integer)fStack.removeFirst()).intValue();}" + //$NON-NLS-1$
      "public void push(int elem){fStack.addFirst(new Integer(elem));}" + //$NON-NLS-1$
      "public boolean isEmpty() {return fStack.isEmpty();}" + //$NON-NLS-1$
      "}"; //$NON-NLS-1$

  /** The PHP Preview. */
  private PHPPreview phpPreview;

  public PHPCodeFormatterConfigurationBlock(final IProject project, final PreferencesAccess access) {
    this(project, access, DIALOGSTORE_LASTSAVELOADPATH);
  }

  /**
   * @param project
   * @param preferenceAccess
   * @param lastSaveLoadPathKey
   */
  public PHPCodeFormatterConfigurationBlock(final IProject project, final PreferencesAccess preferenceAccess,
      final String lastSaveLoadPathKey) {
    super(project, preferenceAccess, lastSaveLoadPathKey);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected void configurePreview(final Composite composite, final int numColumns,
      final PHPProfileManager profileManager) {
    ControlUtils.createLabel(composite, PHPFormatterMessages.CodingStyleConfigurationBlock_preview_label_text,
        numColumns);
    //    PHPProgramUnitPreview result= new PHPProgramUnitPreview(profileManager.getSelected().getSettings(), composite);
    //    result.setPreviewText(PREVIEW);
    //    phpPreview= result;

    final GridData gd = new GridData(GridData.FILL_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL);
    gd.horizontalSpan = numColumns;
    gd.verticalSpan = 7;
    gd.widthHint = 0;
    gd.heightHint = 0;
    //    phpPreview.getControl().setLayoutData(gd);
    new PreviewController(profileManager);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected PHPProfileManager createProfileManager(final List<PHPProfile> profiles, final IScopeContext context,
      final PreferencesAccess preferenceAccess, final PHPProfileVersioner profileVersioner) {
    return new PHPFormatterProfileManager(profiles, context, preferenceAccess, profileVersioner);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected PHPProfileStore createProfileStore(PHPProfileVersioner versioner) {
    return new PHPFormatterProfileStore(versioner);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected PHPProfileVersioner createProfileVersioner() {
    return new PHPProfileVersioner();
  }

  /**
   * {@inheritDoc}
   *
   * @see org.eclipse.php.internal.ui.preferences.formatter.ProfileConfigurationBlock#createModifyDialog(Shell,
   *      IProfile, IProfileManager, IProfileStore, boolean)
   */
  @Override
  protected FormatterModifyDialog createModifyDialog(Shell shell, IProfile profile, IProfileManager profileManager,
      IProfileStore profileStore, boolean newProfile) {
    return null;
    //return new FormatterModifyDialog(shell, profile, profileManager, profileStore, newProfile,
    //    FORMATTER_DIALOG_PREFERENCE_KEY, DIALOGSTORE_LASTSAVELOADPATH);
  }
}
